#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2014年11月5日

@author: chenyongbing
'''
import os,sys
current_dir = os.path.split(os.path.realpath(__file__))[0]

sys.path.append(os.path.join(current_dir,'../../'))
from base.localConfig import baseLocalConfig
import os,re,tarfile

from base.strChar import *
import datetime




from base.PyEmail import *
def openTarFile(rawfile_tar):
    output = ''
    if not os.path.exists(rawfile_tar):
        return output
    tar = tarfile.open(rawfile_tar)
    
    for member in tar.getmembers():
        f = tar.extractfile(member)
        out = f.read()
        output = output + out+'\n'
    tar.close()
    return output


def get_macs_from_rawdata(rawfile=None,output=None):
    macs = []
    if rawfile!=None:
        output = openTarFile(rawfile)
    elif output!=None:
        pass
    else:return []
    for line in output.split('\n'):
        lineList = line.split('.')
        if len(lineList)>3:
            mac = lineList[2]
            if mac not in macs:
                macs.append(mac)
    return macs

def check_mac_global_or_local(mac):
    b = hex2bin(mac[:2])
    #print mac[:2], b
    if int(b[6])==0:
        return 'global'
    else:
        return 'local'

def get_slist_between_st_et(st,et):
    u'''获取2个时间间的所有日期'''
    dlist = []
    count = 0
    nday = (datetime.date(int(st[:4]),int(st[5:7]),int(st[8:]))+ datetime.timedelta(-1)).strftime('%Y-%m-%d')
    while nday < et:
        nday = (datetime.date(int(st[:4]),int(st[5:7]),int(st[8:]))+ datetime.timedelta(count)).strftime('%Y-%m-%d')
        count+=1
        dlist.append(nday)
    return dlist


def get_macs_by_group_file_all(rawdata_path,startTime,endTime=None):
    if endTime==None:endTime=startTime
    groups = []
    msgs = []
    gMsgs = {}
    for day in get_slist_between_st_et(startTime,endTime):
        group_raw_path = rawdata_path+'/rawdata_group/%s'%day

        if not os.path.exists(group_raw_path):
            continue
            #return msgs
        for g_f_t in os.listdir(group_raw_path):
            if not re.search('.txt.tar.gz',g_f_t):continue
            group = re.sub('.txt.tar.gz','',g_f_t)
            group_name = myAnalyzeConfig.getGroupName(shop_group=group)
            groups.append(group)
            macs = get_macs_from_rawdata(rawfile=group_raw_path+'/'+g_f_t)
            g_count = 0;l_count = 0
            for mac in macs:
                type = check_mac_global_or_local(mac)
                if type=='global':g_count+=1
                else:l_count+=1
            if group not in gMsgs.keys():
                gMsgs[group] = {}
            if day not in gMsgs[group].keys():
                gMsgs[group][day] = {}
            gMsgs[group][day]={'global':g_count,'local':l_count}
            #g_rate = str((round(float(g_count)/len(macs),4))*100)+'%'
            #l_rate = str((round(float(l_count)/len(macs),4))*100)+'%'
            #msg = "%s(%s)\tglobal_mac_count:%s[%s]\tlocal_mac_count:%s[%s]"%(group_name,group,g_count,g_rate,l_count,l_rate)
            #msgs.append(msg)
    t_g_count = 0
    t_l_count = 0
    t_t_count = 0
    msgs = []
    for group , daysMsgs in gMsgs.iteritems():
        group_name = myAnalyzeConfig.getGroupName(shop_group=group)
        g_count = 0
        l_count = 0
        for day , dMsgs in daysMsgs.iteritems():
            g_count = g_count+ dMsgs['global']
            l_count = l_count +dMsgs['local'] 
        t_count = g_count+l_count
        g_rate = str((round(float(g_count)/t_count,4))*100)+'%'
        l_rate = str((round(float(l_count)/t_count,4))*100)+'%'
        msg = "%s(%s)\tglobal_mac_count:%s[%s]\tlocal_mac_count:%s[%s]"%(group_name,group,g_count,g_rate,l_count,l_rate)
        msgs.append(msg)
        t_g_count = t_g_count + g_count
        t_l_count = t_l_count + l_count
    t_t_count = t_g_count+t_l_count
    t_g_rate = str((round(float(t_g_count)/t_t_count,4))*100)+'%'
    t_l_rate = str((round(float(t_l_count)/t_t_count,4))*100)+'%'
    msg = "total global_mac_count:%s[%s]\tlocal_mac_count:%s[%s]"%(t_g_count,t_g_rate,t_l_count,t_l_rate)
    msgs = [msg] + [''] + msgs
    msgs = [u'统计时间:%s-%s'%(startTime,endTime)] + [''] + msgs
    return msgs

def get_macs_by_group_file(rawdata_path,day):

    groups = []
    msgs = []
    t_g_count = 0
    t_l_count = 0
    group_raw_path = rawdata_path+'/rawdata_group/%s'%day

    if not os.path.exists(group_raw_path):
        #continue
        return msgs
    for g_f_t in os.listdir(group_raw_path):
        if not re.search('.txt.tar.gz',g_f_t):continue
        group = re.sub('.txt.tar.gz','',g_f_t)
        group_name = myAnalyzeConfig.getGroupName(shop_group=group)
        groups.append(group)
        macs = get_macs_from_rawdata(rawfile=group_raw_path+'/'+g_f_t)
        g_count = 0;l_count = 0
        for mac in macs:
            type = check_mac_global_or_local(mac)
            if type=='global':g_count+=1
            else:l_count+=1
        g_rate = str((round(float(g_count)/len(macs),4))*100)+'%'
        l_rate = str((round(float(l_count)/len(macs),4))*100)+'%'
        msg = "%s(%s)\tglobal_mac_count:%s[%s]\tlocal_mac_count:%s[%s]"%(group_name,group,g_count,g_rate,l_count,l_rate)
        t_g_count = t_g_count + g_count
        t_l_count = t_l_count + l_count
        msgs.append(msg)
    t_t_count = t_g_count + t_l_count
    t_g_rate = str((round(float(t_g_count)/t_t_count,4))*100)+'%'
    t_l_rate = str((round(float(t_l_count)/t_t_count,4))*100)+'%'
    msg = "total global_mac_count:%s[%s]\tlocal_mac_count:%s[%s]"%(t_g_count,t_g_rate,t_l_count,t_l_rate)
    msgs = [msg] + [''] + msgs
    return msgs


def get_macs_from_mysql(day=None,group=None):
    if day==None:
        day = (datetime.date.today()-datetime.timedelta(1)).strftime('%Y-%m-%d') 
    if group==None:
        query = 'select dgroup, mac from (select dgroup, mac from mac_customer_stats where day="%s" union select dgroup, mac from mac_walkby_stats  where day="%s") as t'%(day,day)
    else:
        query = 'select dgroup, mac from (select dgroup, mac from mac_customer_stats where day="%s" and dgroup="%s" union select dgroup, mac from mac_walkby_stats  where day="%s" and dgroup="%s" ) as t'%(day,group,day,group)
    print query
    rets = myZhimind.SelectAll(query)
    if not rets or len(rets)==0:
        print ""
    datas = {}
    for group,mac in rets:
        type = check_mac_global_or_local(mac)
        if not group in datas.keys():
            datas[group] = {'local':[],'global':[]}
        macs = datas[group][type]
        if mac not in macs:
            macs.append(mac)
            datas[group][type] = macs
    msgs = []
    t_g_count = 0
    t_l_count = 0
    for group,data in datas.iteritems():
        group_name = myAnalyzeConfig.getGroupName(shop_group=group).encode('utf-8')
        msg = ''
        g_count = len(data['global'])
        l_count = len(data['local'])
        t_g_count+=g_count
        t_l_count+=l_count
        t_count = g_count + l_count
        g_rate = str((round(float(g_count)/t_count,4))*100)+'%'
        l_rate = str((round(float(l_count)/t_count,4))*100)+'%'
        msg = "%s(%s)\tglobal_mac_count:%s[%s]\tlocal_mac_count:%s[%s]"%(group_name,group,g_count,g_rate,l_count,l_rate)
        msgs.append(msg)

    t_t_count = t_g_count + t_l_count
    t_g_rate = str((round(float(t_g_count)/t_t_count,4))*100)+'%'
    t_l_rate = str((round(float(t_l_count)/t_t_count,4))*100)+'%'
    msg = "total global_mac_count:%s[%s]\tlocal_mac_count:%s[%s]"%(t_g_count,t_g_rate,t_l_count,t_l_rate)
    msgs = [msg] + [''] + msgs
    msgs = ['统计时间:%s'%(day)] + [''] + msgs
    return msgs
        
if __name__ == '__main__':
    for mac in ['58:54:00:d1:c9:97',"58:54:00:d1:c9:98","5e:54:00:d1:c9:98","5e:54:00:d1:c9:96"]:
        print mac,check_mac_global_or_local(mac)
    
    sys.exit()
    from analyzeConfig import AnalyzeConfig
    myAnalyzeConfig = AnalyzeConfig()
    import argparse
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--day', metavar=u'day', default=None)
    parser.add_argument('--startTime', metavar=u'day', default=None)
    parser.add_argument('--endTime', metavar=u'day', default=None)
    parser.add_argument('--email', metavar=u'email', default='chen.yongbing@zhimatech.com')
    parser.add_argument('--source', metavar=u'source ', default='rawdata')
    parser.add_argument('--type', metavar=u'type:all\each ', default='all')
    args = parser.parse_args()
    day = args.day
    email = args.email
    startTime= args.startTime
    endTime = args.endTime
    source = args.source
    type = args.type
    if source =='mysql':
        from PyMySQL import ConnectMySQL

        zhimind_host = myLocalConfig.mysqlHosts['zhimind']['host']
        zhimind_user = myLocalConfig.mysqlHosts['zhimind']['user']
        zhimind_passwd = myLocalConfig.mysqlHosts['zhimind']['passwd']
        zhimind_db = myLocalConfig.mysqlHosts['zhimind']['db']
        
        myZhimind = ConnectMySQL(zhimind_host,zhimind_user,zhimind_passwd,zhimind_db)
    
   
    if startTime!=None and endTime==None:
        endTime = startTime
    if day==None and startTime==None:
        day = (datetime.date.today()-datetime.timedelta(1)).strftime('%Y-%m-%d')
    rawdata_path = myLocalConfig.rawdata_path
    mail_user = myLocalConfig.email_user
    mail_host = myLocalConfig.email_smtp
    mail_pass = myLocalConfig.email_passwd
    mail_postfix = myLocalConfig.email_postfix
    to_list = email.split(',')
    sub = '探测到的mac地址分类'    
    if type=='all':
        if source=='rawdata':
            if startTime!=None and endTime!=None:
                msgs = get_macs_by_group_file_all(rawdata_path,startTime,endTime)
            else:
                msgs = get_macs_by_group_file(rawdata_path,day)
        elif source=='mysql':
            msgs = get_macs_from_mysql(day=day)
        content = '\n'.join(msgs)
        send_mail(mail_user, mail_pass, mail_host, mail_postfix, to_list, sub,content)
    elif type=='each':
        if startTime!=None and endTime!=None:
            for tday in get_slist_between_st_et(startTime,endTime):
                print "day:",tday
                msgs = get_macs_by_group_file_all(rawdata_path,tday,tday)
                content = '\n'.join(msgs)
                send_mail(mail_user, mail_pass, mail_host, mail_postfix, to_list, sub,content)